#include "ac_dialog.h"

BEGIN_EVENT_TABLE(AccountDialog, wxDialog)
	EVT_BUTTON(account_ok,AccountDialog::ac_OkBtn_clicked)
	EVT_BUTTON(account_can,AccountDialog::ac_CanBtn_clicked)
	EVT_BUTTON(account_add,AccountDialog::ac_AddAccount_clicked)
	EVT_BUTTON(account_mod,AccountDialog::ac_ModAccount_clicked)
	EVT_BUTTON(account_del,AccountDialog::ac_DelAccount_clicked)
	EVT_LISTBOX(account_list,AccountDialog::ac_AccountList_selected)
END_EVENT_TABLE() 

AccountDialog::AccountDialog(wxWindow *parent)
:wxDialog(parent, wxID_ANY, _("Setting of Account"), wxDefaultPosition, wxDefaultSize)
{
#ifdef __WXMSW__
	SetClientSize(wxSize(400,240));
#else
	SetClientSize(wxSize(400,260));
#endif


	ac_AccountList = new wxListBox(this,account_list,wxDefaultPosition,wxSize(160,180),0,NULL);
	ac_AddAccount = new wxButton(this, account_add, _("Add"), wxPoint(-1,-1),wxSize(120,-1));
	ac_ModAccount = new wxButton(this, account_mod, _("Modify"), wxPoint(-1,-1),wxSize(120,-1));
	ac_DelAccount = new wxButton(this, account_del, _("Remove"), wxPoint(-1,-1),wxSize(120,-1));
	
	ac_AccountInfo = new wxStaticBox(this, wxID_ANY, _("Infomation of Account"));
	wxStaticText *label1 = new wxStaticText(this,wxID_ANY,_("Account name:"));
	ac_InputAccountName = new wxTextCtrl(this, wxID_ANY, _T(""),wxDefaultPosition,wxSize(120,-1));
	
	wxStaticText *label2 = new wxStaticText(this,wxID_ANY,_("Initial remainder:"));
	ac_InitialRemainder = new wxSpinCtrl(this, wxID_ANY, _T("0"),wxDefaultPosition,wxSize(120,-1),wxSP_ARROW_KEYS,0,INT_MAX,0);

	ac_OkBtn = new wxButton(this, account_ok, _T("OK"), wxPoint(-1,-1),wxSize(85,-1));
	ac_CanBtn = new wxButton(this, account_can, _("Cancel"), wxPoint(-1,-1),wxSize(85,-1));
	
	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
		bsizer2->Add(ac_AccountList,0,wxTOP|wxLEFT,0);
			wxBoxSizer *bsizer3 = new wxBoxSizer(wxVERTICAL);
				wxStaticBoxSizer *sbsizer = new wxStaticBoxSizer(ac_AccountInfo,wxVERTICAL);
				sbsizer->Add(ac_AddAccount,0,wxTOP|wxLEFT,0);
				sbsizer->Add(ac_ModAccount,0,wxTOP|wxLEFT,0);
				sbsizer->Add(ac_DelAccount,0,wxTOP|wxLEFT,0);
				sbsizer->Add(label1,0,wxTOP,15);
				sbsizer->Add(ac_InputAccountName,0,wxTOP|wxLEFT,0);
				sbsizer->Add(label2,0,wxTOP,5);
				sbsizer->Add(ac_InitialRemainder,0,wxTOP|wxLEFT,0);
			bsizer3->Add(sbsizer,0,wxTOP|wxLEFT,0);
		bsizer2->Add(bsizer3,0,wxTOP|wxLEFT,5);
	bsizer1->Add(bsizer2,0,wxBOTTOM,5);
		wxBoxSizer *bsizer4 = new wxBoxSizer(wxHORIZONTAL);
		bsizer4->Add(ac_OkBtn,0,wxTOP|wxLEFT,0);
		bsizer4->Add(ac_CanBtn,0,wxLEFT,5);
	bsizer1->Add(bsizer4,0,wxBOTTOM|wxRIGHT|wxALIGN_RIGHT,10);
	this->SetSizer(bsizer1);
	this->Layout();
		
}
void AccountDialog::ac_OkBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(account_ok);
	this->Hide();
}
void AccountDialog::ac_CanBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(account_can);
	this->Hide();
}
void AccountDialog::ac_AddAccount_clicked(wxCommandEvent &event)
{
	if (ac_InputAccountName->IsEmpty()) {
		wxMessageBox(_("You don't to input the Account name."),APPTITLE,
			wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	DB_account db;
	db.SetFlag(add_mt);
	db.SetId((int)dbaccount.size());
	db.SetRemain(ac_InitialRemainder->GetValue());
	db.SetName(ac_InputAccountName->GetValue());
	dbaccount.push_back(db);
	ac_AccountList->Append(ac_InputAccountName->GetValue());
	ClearControl();
}
void AccountDialog::ac_ModAccount_clicked(wxCommandEvent &event)
{
	if (ac_InputAccountName->IsEmpty()) {
		wxMessageBox(_("You don't to input the Account name."),APPTITLE,
			wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	int sel = ac_AccountList->GetSelection();
	if (dbaccount.at(sel).GetFlag() != add_mt) {
		dbaccount.at(sel).SetFlag(mod_mt);
	}
	dbaccount.at(sel).SetName(ac_InputAccountName->GetValue());
	dbaccount.at(sel).SetRemain(ac_InitialRemainder->GetValue());
	ac_AccountList->SetString(sel,ac_InputAccountName->GetValue());
}
void AccountDialog::ac_DelAccount_clicked(wxCommandEvent &event)
{
	int sel = ac_AccountList->GetSelection();
	if (sel == 0) {
		wxMessageBox(_("You can not remove this account.\n(Because it is default handling)"),APPTITLE,wxOK|wxICON_ERROR,this);
		return;
	}
	if (dbaccount.at(sel).GetFlag() == add_mt) {
		dbaccount.erase(dbaccount.begin()+sel);
	}else{
		dbaccount.at(sel).SetFlag(del_mt);
	}
	ac_AccountList->Delete(sel);
	ClearControl();
}
void AccountDialog::ac_AccountList_selected(wxCommandEvent &event)
{
	int sel = event.GetSelection();
	if (sel >= 0) {
		ac_InputAccountName->SetValue(dbaccount.at(sel).GetName());
		ac_InitialRemainder->SetValue(dbaccount.at(sel).GetRemain());
	}
}
void AccountDialog::BindData_Account(wxSQLite3ResultSet &table)
{
	ac_AccountList->Clear();
	dbaccount.clear();
	while (table.NextRow()) {
		DB_account db;
		db.SetFlag(none_mt);
		db.SetId(table.GetInt(0));
		db.SetName(table.GetString(1));
		db.SetOldName(table.GetString(1));
		db.SetRemain(table.GetInt(2));
		dbaccount.push_back(db);
		ac_AccountList->Append(db.GetName());
	}
}
std::vector<DB_account> AccountDialog::GetChangedData(modifyType mt)
{
	std::vector<DB_account> result;
	int i = 0;
	for (i = 0; i < (int)dbaccount.size(); i++) {
		if ((mt == none_mt) && (dbaccount.at(i).GetFlag() != mt)) {
			result.push_back(dbaccount.at(i));
		}else{
			if (dbaccount.at(i).GetFlag() == mt) {
				result.push_back(dbaccount.at(i));
			}
		}
	}
	return result;

}
